package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CureTaskCourseDAO;
import cn.edu.scau.cmi.hombio.dao.CustomerDAO;
import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.ExceptDAO;
import cn.edu.scau.cmi.hombio.dao.FeedDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTakeDAO;
import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineTakeDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionDAO;
import cn.edu.scau.cmi.hombio.dao.SaleDAO;
import cn.edu.scau.cmi.hombio.dao.SaleTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskEmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskIntervalCycleDAO;
import cn.edu.scau.cmi.hombio.dao.TaskManageDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPighouseDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPreventionDAO;
import cn.edu.scau.cmi.hombio.dao.TaskStatusTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TransferDAO;

import cn.edu.scau.cmi.hombio.domain.CureTaskCourse;
import cn.edu.scau.cmi.hombio.domain.Customer;
import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Except;
import cn.edu.scau.cmi.hombio.domain.Feed;
import cn.edu.scau.cmi.hombio.domain.FodderTake;
import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.MedicineTake;
import cn.edu.scau.cmi.hombio.domain.Prevention;
import cn.edu.scau.cmi.hombio.domain.Sale;
import cn.edu.scau.cmi.hombio.domain.SaleTaskQuality;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskEmployee;
import cn.edu.scau.cmi.hombio.domain.TaskIntervalCycle;
import cn.edu.scau.cmi.hombio.domain.TaskManage;
import cn.edu.scau.cmi.hombio.domain.TaskPig;
import cn.edu.scau.cmi.hombio.domain.TaskPighouse;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;
import cn.edu.scau.cmi.hombio.domain.TaskPrevention;
import cn.edu.scau.cmi.hombio.domain.TaskStatusType;
import cn.edu.scau.cmi.hombio.domain.TaskType;
import cn.edu.scau.cmi.hombio.domain.Transfer;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Task entities
 * 
 */

@Service("TaskService")

@Transactional
public class TaskServiceImpl implements TaskService {

	/**
	 * DAO injected by Spring that manages CureTaskCourse entities
	 * 
	 */
	@Autowired
	private CureTaskCourseDAO cureTaskCourseDAO;

	/**
	 * DAO injected by Spring that manages Customer entities
	 * 
	 */
	@Autowired
	private CustomerDAO customerDAO;

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Except entities
	 * 
	 */
	@Autowired
	private ExceptDAO exceptDAO;

	/**
	 * DAO injected by Spring that manages Feed entities
	 * 
	 */
	@Autowired
	private FeedDAO feedDAO;

	/**
	 * DAO injected by Spring that manages FodderTake entities
	 * 
	 */
	@Autowired
	private FodderTakeDAO fodderTakeDAO;

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages MedicineTake entities
	 * 
	 */
	@Autowired
	private MedicineTakeDAO medicineTakeDAO;

	/**
	 * DAO injected by Spring that manages Prevention entities
	 * 
	 */
	@Autowired
	private PreventionDAO preventionDAO;

	/**
	 * DAO injected by Spring that manages Sale entities
	 * 
	 */
	@Autowired
	private SaleDAO saleDAO;

	/**
	 * DAO injected by Spring that manages SaleTaskQuality entities
	 * 
	 */
	@Autowired
	private SaleTaskQualityDAO saleTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskEmployee entities
	 * 
	 */
	@Autowired
	private TaskEmployeeDAO taskEmployeeDAO;

	/**
	 * DAO injected by Spring that manages TaskIntervalCycle entities
	 * 
	 */
	@Autowired
	private TaskIntervalCycleDAO taskIntervalCycleDAO;

	/**
	 * DAO injected by Spring that manages TaskManage entities
	 * 
	 */
	@Autowired
	private TaskManageDAO taskManageDAO;

	/**
	 * DAO injected by Spring that manages TaskPig entities
	 * 
	 */
	@Autowired
	private TaskPigDAO taskPigDAO;

	/**
	 * DAO injected by Spring that manages TaskPighouse entities
	 * 
	 */
	@Autowired
	private TaskPighouseDAO taskPighouseDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * DAO injected by Spring that manages TaskPrevention entities
	 * 
	 */
	@Autowired
	private TaskPreventionDAO taskPreventionDAO;

	/**
	 * DAO injected by Spring that manages TaskStatusType entities
	 * 
	 */
	@Autowired
	private TaskStatusTypeDAO taskStatusTypeDAO;

	/**
	 * DAO injected by Spring that manages TaskType entities
	 * 
	 */
	@Autowired
	private TaskTypeDAO taskTypeDAO;

	/**
	 * DAO injected by Spring that manages Transfer entities
	 * 
	 */
	@Autowired
	private TransferDAO transferDAO;

	/**
	 * Instantiates a new TaskServiceImpl.
	 *
	 */
	public TaskServiceImpl() {
	}

	/**
	 */
	@Transactional
	public Task findTaskByPrimaryKey(Integer id) {
		return taskDAO.findTaskByPrimaryKey(id);
	}

	/**
	 * Return all Task entity
	 * 
	 */
	@Transactional
	public List<Task> findAllTasks(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Task>(taskDAO.findAllTasks(startResult, maxRows));
	}

	/**
	 * Save an existing TaskEmployee entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskEmployees(Integer id, TaskEmployee related_taskemployees) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskEmployee existingtaskEmployees = taskEmployeeDAO.findTaskEmployeeByPrimaryKey(related_taskemployees.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskEmployees != null) {
			existingtaskEmployees.setId(related_taskemployees.getId());
			related_taskemployees = existingtaskEmployees;
		}

		related_taskemployees.setTask(task);
		task.getTaskEmployees().add(related_taskemployees);
		related_taskemployees = taskEmployeeDAO.store(related_taskemployees);
		taskEmployeeDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Load an existing Task entity
	 * 
	 */
	@Transactional
	public Set<Task> loadTasks() {
		return taskDAO.findAllTasks();
	}

	/**
	 * Delete an existing TaskEmployee entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskEmployees(Integer task_id, Integer related_taskemployees_id) {
		TaskEmployee related_taskemployees = taskEmployeeDAO.findTaskEmployeeByPrimaryKey(related_taskemployees_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_taskemployees.setTask(null);
		task.getTaskEmployees().remove(related_taskemployees);

		taskEmployeeDAO.remove(related_taskemployees);
		taskEmployeeDAO.flush();

		return task;
	}

	/**
	 * Save an existing Sale entity
	 * 
	 */
	@Transactional
	public Task saveTaskSales(Integer id, Sale related_sales) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Sale existingsales = saleDAO.findSaleByPrimaryKey(related_sales.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsales != null) {
			existingsales.setId(related_sales.getId());
			existingsales.setSaleType(related_sales.getSaleType());
			existingsales.setMemo(related_sales.getMemo());
			related_sales = existingsales;
		}

		related_sales.setTask(task);
		task.getSales().add(related_sales);
		related_sales = saleDAO.store(related_sales);
		saleDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Save an existing Except entity
	 * 
	 */
	@Transactional
	public Task saveTaskExcepts(Integer id, Except related_excepts) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Except existingexcepts = exceptDAO.findExceptByPrimaryKey(related_excepts.getId());

		// copy into the existing record to preserve existing relationships
		if (existingexcepts != null) {
			existingexcepts.setId(related_excepts.getId());
			existingexcepts.setBarcode(related_excepts.getBarcode());
			existingexcepts.setReportTime(related_excepts.getReportTime());
			existingexcepts.setMemo(related_excepts.getMemo());
			related_excepts = existingexcepts;
		} else {
			related_excepts = exceptDAO.store(related_excepts);
			exceptDAO.flush();
		}

		related_excepts.setTask(task);
		task.getExcepts().add(related_excepts);
		related_excepts = exceptDAO.store(related_excepts);
		exceptDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Save an existing Prevention entity
	 * 
	 */
	@Transactional
	public Task saveTaskPreventions(Integer id, Prevention related_preventions) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Prevention existingpreventions = preventionDAO.findPreventionByPrimaryKey(related_preventions.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpreventions != null) {
			existingpreventions.setId(related_preventions.getId());
			existingpreventions.setTime(related_preventions.getTime());
			existingpreventions.setMemo(related_preventions.getMemo());
			related_preventions = existingpreventions;
		}

		related_preventions.setTask(task);
		task.getPreventions().add(related_preventions);
		related_preventions = preventionDAO.store(related_preventions);
		preventionDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Feed entity
	 * 
	 */
	@Transactional
	public Task deleteTaskFeeds(Integer task_id, Integer related_feeds_id) {
		Feed related_feeds = feedDAO.findFeedByPrimaryKey(related_feeds_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_feeds.setTask(null);
		task.getFeeds().remove(related_feeds);

		feedDAO.remove(related_feeds);
		feedDAO.flush();

		return task;
	}

	/**
	 * Save an existing TaskPrevention entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskPreventions(Integer id, TaskPrevention related_taskpreventions) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskPrevention existingtaskPreventions = taskPreventionDAO.findTaskPreventionByPrimaryKey(related_taskpreventions.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPreventions != null) {
			existingtaskPreventions.setId(related_taskpreventions.getId());
			existingtaskPreventions.setMemo(related_taskpreventions.getMemo());
			related_taskpreventions = existingtaskPreventions;
		}

		related_taskpreventions.setTask(task);
		task.getTaskPreventions().add(related_taskpreventions);
		related_taskpreventions = taskPreventionDAO.store(related_taskpreventions);
		taskPreventionDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Save an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public Task saveTaskCureTaskCourses(Integer id, CureTaskCourse related_curetaskcourses) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		CureTaskCourse existingcureTaskCourses = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourses.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskCourses != null) {
			existingcureTaskCourses.setId(related_curetaskcourses.getId());
			existingcureTaskCourses.setStep(related_curetaskcourses.getStep());
			existingcureTaskCourses.setTime(related_curetaskcourses.getTime());
			existingcureTaskCourses.setMemo(related_curetaskcourses.getMemo());
			related_curetaskcourses = existingcureTaskCourses;
		}

		related_curetaskcourses.setTask(task);
		task.getCureTaskCourses().add(related_curetaskcourses);
		related_curetaskcourses = cureTaskCourseDAO.store(related_curetaskcourses);
		cureTaskCourseDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing TaskPrevention entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskPreventions(Integer task_id, Integer related_taskpreventions_id) {
		TaskPrevention related_taskpreventions = taskPreventionDAO.findTaskPreventionByPrimaryKey(related_taskpreventions_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_taskpreventions.setTask(null);
		task.getTaskPreventions().remove(related_taskpreventions);

		taskPreventionDAO.remove(related_taskpreventions);
		taskPreventionDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Inspection entity
	 * 
	 */
	@Transactional
	public Task deleteTaskInspections(Integer task_id, Integer related_inspections_id) {
		Inspection related_inspections = inspectionDAO.findInspectionByPrimaryKey(related_inspections_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_inspections.setTask(null);
		task.getInspections().remove(related_inspections);

		inspectionDAO.remove(related_inspections);
		inspectionDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public Task deleteTaskEmployee(Integer task_id, Integer related_employee_id) {
		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		task.setEmployee(null);
		related_employee.getTasks().remove(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Sale entity
	 * 
	 */
	@Transactional
	public Task deleteTaskSales(Integer task_id, Integer related_sales_id) {
		Sale related_sales = saleDAO.findSaleByPrimaryKey(related_sales_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_sales.setTask(null);
		task.getSales().remove(related_sales);

		saleDAO.remove(related_sales);
		saleDAO.flush();

		return task;
	}

	/**
	 * Delete an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public Task deleteTaskMedicineTakes(Integer task_id, Integer related_medicinetakes_id) {
		MedicineTake related_medicinetakes = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetakes_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_medicinetakes.setTask(null);
		task.getMedicineTakes().remove(related_medicinetakes);

		medicineTakeDAO.remove(related_medicinetakes);
		medicineTakeDAO.flush();

		return task;
	}

	/**
	 * Save an existing FodderTake entity
	 * 
	 */
	@Transactional
	public Task saveTaskFodderTakes(Integer id, FodderTake related_foddertakes) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		FodderTake existingfodderTakes = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakes.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderTakes != null) {
			existingfodderTakes.setId(related_foddertakes.getId());
			existingfodderTakes.setQuantity(related_foddertakes.getQuantity());
			existingfodderTakes.setTime(related_foddertakes.getTime());
			existingfodderTakes.setMemo(related_foddertakes.getMemo());
			related_foddertakes = existingfodderTakes;
		}

		related_foddertakes.setTask(task);
		task.getFodderTakes().add(related_foddertakes);
		related_foddertakes = fodderTakeDAO.store(related_foddertakes);
		fodderTakeDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Save an existing TaskStatusType entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskStatusType(Integer id, TaskStatusType related_taskstatustype) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskStatusType existingtaskStatusType = taskStatusTypeDAO.findTaskStatusTypeByPrimaryKey(related_taskstatustype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskStatusType != null) {
			existingtaskStatusType.setId(related_taskstatustype.getId());
			existingtaskStatusType.setName(related_taskstatustype.getName());
			existingtaskStatusType.setMemo(related_taskstatustype.getMemo());
			related_taskstatustype = existingtaskStatusType;
		}

		task.setTaskStatusType(related_taskstatustype);
		related_taskstatustype.getTasks().add(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_taskstatustype = taskStatusTypeDAO.store(related_taskstatustype);
		taskStatusTypeDAO.flush();

		return task;
	}

	/**
	 * Save an existing TaskType entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskType(Integer id, TaskType related_tasktype) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskType existingtaskType = taskTypeDAO.findTaskTypeByPrimaryKey(related_tasktype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskType != null) {
			existingtaskType.setId(related_tasktype.getId());
			existingtaskType.setName(related_tasktype.getName());
			existingtaskType.setMemo(related_tasktype.getMemo());
			related_tasktype = existingtaskType;
		}

		task.setTaskType(related_tasktype);
		related_tasktype.getTasks().add(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_tasktype = taskTypeDAO.store(related_tasktype);
		taskTypeDAO.flush();

		return task;
	}

	/**
	 * Delete an existing TaskPighouse entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskPighouses(Integer task_id, Integer related_taskpighouses_id) {
		TaskPighouse related_taskpighouses = taskPighouseDAO.findTaskPighouseByPrimaryKey(related_taskpighouses_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_taskpighouses.setTask(null);
		task.getTaskPighouses().remove(related_taskpighouses);

		taskPighouseDAO.remove(related_taskpighouses);
		taskPighouseDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Customer entity
	 * 
	 */
	@Transactional
	public Task deleteTaskCustomer(Integer task_id, Integer related_customer_id) {
		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);
		Customer related_customer = customerDAO.findCustomerByPrimaryKey(related_customer_id, -1, -1);

		task.setCustomer(null);
		related_customer.getTasks().remove(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_customer = customerDAO.store(related_customer);
		customerDAO.flush();

		customerDAO.remove(related_customer);
		customerDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Transfer entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTransfers(Integer task_id, Integer related_transfers_id) {
		Transfer related_transfers = transferDAO.findTransferByPrimaryKey(related_transfers_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_transfers.setTask(null);
		task.getTransfers().remove(related_transfers);

		transferDAO.remove(related_transfers);
		transferDAO.flush();

		return task;
	}

	/**
	 * Delete an existing SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public Task deleteTaskSaleTaskQualities(Integer task_id, Integer related_saletaskqualities_id) {
		SaleTaskQuality related_saletaskqualities = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(related_saletaskqualities_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_saletaskqualities.setTask(null);
		task.getSaleTaskQualities().remove(related_saletaskqualities);

		saleTaskQualityDAO.remove(related_saletaskqualities);
		saleTaskQualityDAO.flush();

		return task;
	}

	/**
	 * Delete an existing TaskPig entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskPigs(Integer task_id, Integer related_taskpigs_id) {
		TaskPig related_taskpigs = taskPigDAO.findTaskPigByPrimaryKey(related_taskpigs_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_taskpigs.setTask(null);
		task.getTaskPigs().remove(related_taskpigs);

		taskPigDAO.remove(related_taskpigs);
		taskPigDAO.flush();

		return task;
	}

	/**
	 * Save an existing Inspection entity
	 * 
	 */
	@Transactional
	public Task saveTaskInspections(Integer id, Inspection related_inspections) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Inspection existinginspections = inspectionDAO.findInspectionByPrimaryKey(related_inspections.getId());

		// copy into the existing record to preserve existing relationships
		if (existinginspections != null) {
			existinginspections.setId(related_inspections.getId());
			existinginspections.setTime(related_inspections.getTime());
			existinginspections.setMemo(related_inspections.getMemo());
			related_inspections = existinginspections;
		}

		related_inspections.setTask(task);
		task.getInspections().add(related_inspections);
		related_inspections = inspectionDAO.store(related_inspections);
		inspectionDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskPigsties(Integer task_id, Integer related_taskpigsties_id) {
		TaskPigsty related_taskpigsties = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_taskpigsties.setTask(null);
		task.getTaskPigsties().remove(related_taskpigsties);

		taskPigstyDAO.remove(related_taskpigsties);
		taskPigstyDAO.flush();

		return task;
	}

	/**
	 * Save an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskPigsties(Integer id, TaskPigsty related_taskpigsties) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskPigsty existingtaskPigsties = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPigsties != null) {
			existingtaskPigsties.setId(related_taskpigsties.getId());
			existingtaskPigsties.setMemo(related_taskpigsties.getMemo());
			related_taskpigsties = existingtaskPigsties;
		}

		related_taskpigsties.setTask(task);
		task.getTaskPigsties().add(related_taskpigsties);
		related_taskpigsties = taskPigstyDAO.store(related_taskpigsties);
		taskPigstyDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Return a count of all Task entity
	 * 
	 */
	@Transactional
	public Integer countTasks() {
		return ((Long) taskDAO.createQuerySingleResult("select count(o) from Task o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing TaskManage entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskManages(Integer id, TaskManage related_taskmanages) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskManage existingtaskManages = taskManageDAO.findTaskManageByPrimaryKey(related_taskmanages.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskManages != null) {
			existingtaskManages.setId(related_taskmanages.getId());
			existingtaskManages.setRemind(related_taskmanages.getRemind());
			related_taskmanages = existingtaskManages;
		}

		related_taskmanages.setTask(task);
		task.getTaskManages().add(related_taskmanages);
		related_taskmanages = taskManageDAO.store(related_taskmanages);
		taskManageDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public void deleteTask(Task task) {
		taskDAO.remove(task);
		taskDAO.flush();
	}

	/**
	 * Delete an existing TaskIntervalCycle entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskIntervalCycles(Integer task_id, Integer related_taskintervalcycles_id) {
		TaskIntervalCycle related_taskintervalcycles = taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(related_taskintervalcycles_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_taskintervalcycles.setTask(null);
		task.getTaskIntervalCycles().remove(related_taskintervalcycles);

		taskIntervalCycleDAO.remove(related_taskintervalcycles);
		taskIntervalCycleDAO.flush();

		return task;
	}

	/**
	 * Save an existing Customer entity
	 * 
	 */
	@Transactional
	public Task saveTaskCustomer(Integer id, Customer related_customer) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Customer existingcustomer = customerDAO.findCustomerByPrimaryKey(related_customer.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcustomer != null) {
			existingcustomer.setId(related_customer.getId());
			existingcustomer.setName(related_customer.getName());
			existingcustomer.setMemo(related_customer.getMemo());
			related_customer = existingcustomer;
		} else {
			related_customer = customerDAO.store(related_customer);
			customerDAO.flush();
		}

		task.setCustomer(related_customer);
		related_customer.getTasks().add(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_customer = customerDAO.store(related_customer);
		customerDAO.flush();

		return task;
	}

	/**
	 * Delete an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public Task deleteTaskCureTaskCourses(Integer task_id, Integer related_curetaskcourses_id) {
		CureTaskCourse related_curetaskcourses = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(related_curetaskcourses_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_curetaskcourses.setTask(null);
		task.getCureTaskCourses().remove(related_curetaskcourses);

		cureTaskCourseDAO.remove(related_curetaskcourses);
		cureTaskCourseDAO.flush();

		return task;
	}

	/**
	 * Save an existing TaskPighouse entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskPighouses(Integer id, TaskPighouse related_taskpighouses) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskPighouse existingtaskPighouses = taskPighouseDAO.findTaskPighouseByPrimaryKey(related_taskpighouses.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPighouses != null) {
			existingtaskPighouses.setId(related_taskpighouses.getId());
			existingtaskPighouses.setMemo(related_taskpighouses.getMemo());
			related_taskpighouses = existingtaskPighouses;
		}

		related_taskpighouses.setTask(task);
		task.getTaskPighouses().add(related_taskpighouses);
		related_taskpighouses = taskPighouseDAO.store(related_taskpighouses);
		taskPighouseDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Save an existing Feed entity
	 * 
	 */
	@Transactional
	public Task saveTaskFeeds(Integer id, Feed related_feeds) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Feed existingfeeds = feedDAO.findFeedByPrimaryKey(related_feeds.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfeeds != null) {
			existingfeeds.setId(related_feeds.getId());
			existingfeeds.setTime(related_feeds.getTime());
			existingfeeds.setMemo(related_feeds.getMemo());
			related_feeds = existingfeeds;
		}

		related_feeds.setTask(task);
		task.getFeeds().add(related_feeds);
		related_feeds = feedDAO.store(related_feeds);
		feedDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing TaskType entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskType(Integer task_id, Integer related_tasktype_id) {
		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);
		TaskType related_tasktype = taskTypeDAO.findTaskTypeByPrimaryKey(related_tasktype_id, -1, -1);

		task.setTaskType(null);
		related_tasktype.getTasks().remove(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_tasktype = taskTypeDAO.store(related_tasktype);
		taskTypeDAO.flush();

		taskTypeDAO.remove(related_tasktype);
		taskTypeDAO.flush();

		return task;
	}

	/**
	 * Delete an existing FodderTake entity
	 * 
	 */
	@Transactional
	public Task deleteTaskFodderTakes(Integer task_id, Integer related_foddertakes_id) {
		FodderTake related_foddertakes = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakes_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_foddertakes.setTask(null);
		task.getFodderTakes().remove(related_foddertakes);

		fodderTakeDAO.remove(related_foddertakes);
		fodderTakeDAO.flush();

		return task;
	}

	/**
	 * Save an existing TaskPig entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskPigs(Integer id, TaskPig related_taskpigs) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskPig existingtaskPigs = taskPigDAO.findTaskPigByPrimaryKey(related_taskpigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPigs != null) {
			existingtaskPigs.setId(related_taskpigs.getId());
			existingtaskPigs.setMemo(related_taskpigs.getMemo());
			related_taskpigs = existingtaskPigs;
		}

		related_taskpigs.setTask(task);
		task.getTaskPigs().add(related_taskpigs);
		related_taskpigs = taskPigDAO.store(related_taskpigs);
		taskPigDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing TaskStatusType entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskStatusType(Integer task_id, Integer related_taskstatustype_id) {
		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);
		TaskStatusType related_taskstatustype = taskStatusTypeDAO.findTaskStatusTypeByPrimaryKey(related_taskstatustype_id, -1, -1);

		task.setTaskStatusType(null);
		related_taskstatustype.getTasks().remove(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_taskstatustype = taskStatusTypeDAO.store(related_taskstatustype);
		taskStatusTypeDAO.flush();

		taskStatusTypeDAO.remove(related_taskstatustype);
		taskStatusTypeDAO.flush();

		return task;
	}

	/**
	 * Save an existing Transfer entity
	 * 
	 */
	@Transactional
	public Task saveTaskTransfers(Integer id, Transfer related_transfers) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Transfer existingtransfers = transferDAO.findTransferByPrimaryKey(related_transfers.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtransfers != null) {
			existingtransfers.setId(related_transfers.getId());
			existingtransfers.setTime(related_transfers.getTime());
			existingtransfers.setMemo(related_transfers.getMemo());
			related_transfers = existingtransfers;
		}

		related_transfers.setTask(task);
		task.getTransfers().add(related_transfers);
		related_transfers = transferDAO.store(related_transfers);
		transferDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Save an existing SaleTaskQuality entity
	 * 
	 */
	@Transactional
	public Task saveTaskSaleTaskQualities(Integer id, SaleTaskQuality related_saletaskqualities) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		SaleTaskQuality existingsaleTaskQualities = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(related_saletaskqualities.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsaleTaskQualities != null) {
			existingsaleTaskQualities.setId(related_saletaskqualities.getId());
			existingsaleTaskQualities.setPrice(related_saletaskqualities.getPrice());
			existingsaleTaskQualities.setQuality(related_saletaskqualities.getQuality());
			existingsaleTaskQualities.setMemo(related_saletaskqualities.getMemo());
			related_saletaskqualities = existingsaleTaskQualities;
		}

		related_saletaskqualities.setTask(task);
		task.getSaleTaskQualities().add(related_saletaskqualities);
		related_saletaskqualities = saleTaskQualityDAO.store(related_saletaskqualities);
		saleTaskQualityDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Except entity
	 * 
	 */
	@Transactional
	public Task deleteTaskExcepts(Integer task_id, Integer related_excepts_id) {
		Except related_excepts = exceptDAO.findExceptByPrimaryKey(related_excepts_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_excepts.setTask(null);
		task.getExcepts().remove(related_excepts);

		exceptDAO.remove(related_excepts);
		exceptDAO.flush();

		return task;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public void saveTask(Task task) {
		Task existingTask = taskDAO.findTaskByPrimaryKey(task.getId());

		if (existingTask != null) {
			if (existingTask != task) {
				existingTask.setId(task.getId());
				existingTask.setTaskNumber(task.getTaskNumber());
				existingTask.setCreateTime(task.getCreateTime());
				existingTask.setStartTime(task.getStartTime());
				existingTask.setEndTime(task.getEndTime());
				existingTask.setMemo(task.getMemo());
			}
			task = taskDAO.store(existingTask);
		} else {
			task = taskDAO.store(task);
		}
		taskDAO.flush();
	}

	/**
	 * Delete an existing TaskManage entity
	 * 
	 */
	@Transactional
	public Task deleteTaskTaskManages(Integer task_id, Integer related_taskmanages_id) {
		TaskManage related_taskmanages = taskManageDAO.findTaskManageByPrimaryKey(related_taskmanages_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_taskmanages.setTask(null);
		task.getTaskManages().remove(related_taskmanages);

		taskManageDAO.remove(related_taskmanages);
		taskManageDAO.flush();

		return task;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public Task saveTaskEmployee(Integer id, Employee related_employee) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		task.setEmployee(related_employee);
		related_employee.getTasks().add(task);
		task = taskDAO.store(task);
		taskDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return task;
	}

	/**
	 * Save an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public Task saveTaskMedicineTakes(Integer id, MedicineTake related_medicinetakes) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		MedicineTake existingmedicineTakes = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetakes.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineTakes != null) {
			existingmedicineTakes.setId(related_medicinetakes.getId());
			existingmedicineTakes.setTime(related_medicinetakes.getTime());
			existingmedicineTakes.setMemo(related_medicinetakes.getMemo());
			related_medicinetakes = existingmedicineTakes;
		}

		related_medicinetakes.setTask(task);
		task.getMedicineTakes().add(related_medicinetakes);
		related_medicinetakes = medicineTakeDAO.store(related_medicinetakes);
		medicineTakeDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Save an existing TaskIntervalCycle entity
	 * 
	 */
	@Transactional
	public Task saveTaskTaskIntervalCycles(Integer id, TaskIntervalCycle related_taskintervalcycles) {
		Task task = taskDAO.findTaskByPrimaryKey(id, -1, -1);
		TaskIntervalCycle existingtaskIntervalCycles = taskIntervalCycleDAO.findTaskIntervalCycleByPrimaryKey(related_taskintervalcycles.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskIntervalCycles != null) {
			existingtaskIntervalCycles.setId(related_taskintervalcycles.getId());
			existingtaskIntervalCycles.setFirstExecuteDate(related_taskintervalcycles.getFirstExecuteDate());
			existingtaskIntervalCycles.setCycleDays(related_taskintervalcycles.getCycleDays());
			existingtaskIntervalCycles.setMemo(related_taskintervalcycles.getMemo());
			related_taskintervalcycles = existingtaskIntervalCycles;
		}

		related_taskintervalcycles.setTask(task);
		task.getTaskIntervalCycles().add(related_taskintervalcycles);
		related_taskintervalcycles = taskIntervalCycleDAO.store(related_taskintervalcycles);
		taskIntervalCycleDAO.flush();

		task = taskDAO.store(task);
		taskDAO.flush();

		return task;
	}

	/**
	 * Delete an existing Prevention entity
	 * 
	 */
	@Transactional
	public Task deleteTaskPreventions(Integer task_id, Integer related_preventions_id) {
		Prevention related_preventions = preventionDAO.findPreventionByPrimaryKey(related_preventions_id, -1, -1);

		Task task = taskDAO.findTaskByPrimaryKey(task_id, -1, -1);

		related_preventions.setTask(null);
		task.getPreventions().remove(related_preventions);

		preventionDAO.remove(related_preventions);
		preventionDAO.flush();

		return task;
	}
}
